<?php
class Vfriend_Plugins_Controller extends Zend_Controller_Plugin_Abstract
{
	public function routeStartup(Zend_Controller_Request_Abstract $objRequest)
	{
	}

	public function routeShutdown(Zend_Controller_Request_Abstract $objRequest)
	{
	}

	public function dispatchLoopStartup(Zend_Controller_Request_Abstract $objRequest)
	{
	}

	public function preDispatch(Zend_Controller_Request_Abstract $objRequest)
	{
		try {
			$objView		= Zend_Registry::get('objView');
			$strController 	= $objRequest->getControllerName();
			$strAction 		= $objRequest->getActionName();
			$objVfriendUser = new Vfriend_User();
			$AppUI 			= Vfriend_Session::start();		
			$strUsername 	= $objRequest->getParam('username', '');
			$intHexId 		= $objRequest->getParam('hexid', 0);		
			$objUser		= null;		
			if ($strUsername == '' && $intHexId == 0 && $AppUI->id > 0) {
				$strUsername = $AppUI->username;
			}		
			if($strUsername) {
				$objUser	= $objVfriendUser->getInfoByUsername($strUsername);
			}elseif($intHexId) {
				$objUser	= $objVfriendUser->getInfoByHexUserId($intHexId);
			}		
			Zend_Registry::set('AppUI'		, $AppUI);
			$objView->assign('AppUI'		, $AppUI);	
			Zend_Registry::set('objUser'	, $objUser);
			$objView->assign('objUser'		, $objUser);
			
			if ($strController == 'statics' ||
				$strController == 'ajax' || 
				$strController == 'valid' || 
				$strController == 'request' || 
				$strController == 'comment' || 
				($strController == 'index' && ($strAction == 'login' || $strAction == 'loginerr' || $strAction == 'logout')) 
			) {					
				return;
			}
			
			/** Set Back URL **/
			$strBackURL = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
			if (!in_array(strrchr($strBackURL, '.'), array('.gif','.png','.jpg','.xml','.ico','.htc','.swf','.css')) && !isset($_GET['error'])) {
				if (strrchr($strBackURL, '.') == '.htc') exit;
				$_SESSION[SESSION_BACKURL] = $strBackURL;
			}		
			 if( $AppUI->id > 0) {			
				$strSettingNotification = Vfriend_Function::getCookie(COOKIE_VFRIEND_SETTING_NOTIFICATION);	
				$strSettingPreference	= Vfriend_Function::getCookie(COOKIE_VFRIEND_SETTING_PREFERENCE);			
				if(isset($strSettingNotification) && (string)$strSettingNotification != '') {
					$arrUnotification = explode('::', $strSettingNotification);
					$isResult	= $objVfriendUser->addUpdateNotification($AppUI->id, $arrUnotification[0], $arrUnotification[1], $arrUnotification[2], 
																			$arrUnotification[3], $arrUnotification[4], $arrUnotification[5], 
																			$arrUnotification[6], $arrUnotification[7], $arrUnotification[8],
																			$arrUnotification[9], $arrUnotification[10], $arrUnotification[11]);					
					if($isResult) {
						Vfriend_Function::deleteCookie(COOKIE_VFRIEND_SETTING_NOTIFICATION);
					}
				}
				if(isset($strSettingPreference) && $strSettingPreference != '') {	
					
					$arrPreference = explode(',', $strSettingPreference);
					$intCount	   = count($arrPreference);
					$objVfriendUser->deleteUPreference($AppUI->id,3);
					for($i=0;$i<$intCount;$i++) {
						$intAnswer = $arrPreference[$i];					
						$objVfriendUser->addUPreference($AppUI->id,0,$intAnswer,3);
						if($i+1 == $intCount) {	
							Vfriend_Function::deleteCookie(COOKIE_VFRIEND_SETTING_PREFERENCE);
						}
					}
				}
			} 
			// Auto login when remember me
			if (isset($_COOKIE[COOKIE_VFRIEND_AUTH]) && $_COOKIE[COOKIE_VFRIEND_AUTH] != '' && $AppUI->id == 0) {
				$arrCookieAuth 		= explode('::', Vfriend_Function::base64D($_COOKIE[COOKIE_VFRIEND_AUTH]));
				$strEmail 			= $arrCookieAuth[0];
				$strPassword 		= $arrCookieAuth[1];
				$intPasswordLength	= $arrCookieAuth[2];
				$intLogin			= $arrCookieAuth[3];
				$objVfriendUser		= new Vfriend_User();
				$strDbPassword		= $objVfriendUser->getPassword($strEmail);
				if(md5($strDbPassword) == $strPassword && $intLogin == 1) {				
					$objAuth 	= new Vfriend_Auth();
					$AppUI 		= $objAuth->authVfriend($strEmail);
					$objVfriendUser->updateLastlogin($AppUI->id);
					Zend_Registry::set('AppUI', $AppUI);
					$objView->assign('AppUI', $AppUI);					
					Vfriend_Function::redirect((isset($_SESSION[SESSION_BACKURL]) && $_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_VFRIEND);
					exit();			
				}
			}
		} catch(Exception $e) {			
		}
	}

	public function postDispatch(Zend_Controller_Request_Abstract $objRequest)
	{
	}

	public function dispatchLoopShutdown()
	{
	}
}
?>